local UI = SecretaryUI

local function GetTableObject(this)
	return this:GetParent().object
end

local function HeaderButton_OnEnter(this)
	local text = GetTableObject(this):GetHeaderDesc(this.col)
	if not text then return end
	GameTooltip:ClearLines()
	GameTooltip:SetOwner(this)
	GameTooltip:AddLine(desc)
	GameTooltip:Show()
end

local function HeaderButton_OnLeave(this)
	if GameTooltip:IsOwned(this) then
		GameTooltip:Hide()
	end
end

local function HeaderButton_OnClick(this)
	GetTableObject(this):OnHeaderClick(this.col)
end

local function HeaderButton_Layout(this)
	this.drag:Layout()
	
	this:ClearAllPoints()
	if this.col == 1 then
		this:SetPoint("TOPLEFT", this:GetParent())
	else
		this:SetPoint("TOPLEFT", GetTableObject(this):GetHeaderFrame(this.col - 1).drag, "TOPLEFT")
	end
	this:SetPoint("TOPRIGHT", this.drag, "TOPLEFT")
	this:SetHeight(GetTableObject(this):GetHeaderHeight())
end

local function MakeHeaderButton(parent, col)
	local header = CreateFrame("Button", parent:GetName().."Header"..col, parent)
	
	header:SetTextFontObject("GameFontNormalSmall")
	
	local icon = header:CreateTexture(nil, "OVERLAY")
	header.icon = icon
	icon:SetWidth(16)
	icon:SetHeight(16)
	icon:SetPoint("RIGHT")

	header.col = col
	header:SetScript("OnEnter", HeaderButton_OnEnter)
	header:SetScript("OnLeave", HeaderButton_OnLeave)
	header:SetScript("OnClick", HeaderButton_OnClick)
	
	header.drag = UI.MakeHeaderDrag(header)
	
	header.Layout = HeaderButton_Layout
	
	return header
end

UI.MakeHeaderButton = MakeHeaderButton